package com.chinasoftware.civilaviation.controller;

import com.chinasoftware.civilaviation.entity.Flightinfo;
import com.chinasoftware.civilaviation.entity.JsonResult;
import com.chinasoftware.civilaviation.mapper.FlightinfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
//@RequestMapping("**.do")
@RequestMapping(value="/admin")
public class FlightInfoController {

    @Autowired
    FlightinfoMapper flightinfoMapper;

    //@RequestMapping(value="goods",method=RequestMethod.GET)
    @GetMapping("restflight")
    public JsonResult<Flightinfo> showGoods(int pageSize, int pageNum,
                                            String sort, String order, String pid, String type){
        PageHelper.startPage(pageNum, pageSize, sort+" "+order);   //sort+" "+order  ===>  cname desc
        PageInfo<Flightinfo> pagedata = null;
        if(pid == "" && type == ""){
            pagedata = new PageInfo<Flightinfo>(flightinfoMapper.selectAll());
        }
        else if(pid == ""){
            pagedata = new PageInfo<Flightinfo>(flightinfoMapper.select(new Flightinfo().setType(type)));
        }
        else if(type == ""){
            pagedata = new PageInfo<Flightinfo>(flightinfoMapper.select(new Flightinfo().setPid(pid)));
        }
        else{
            pagedata = new PageInfo<Flightinfo>(flightinfoMapper.select(new Flightinfo().setPid(pid).setType(type)));
        }
        return new JsonResult<Flightinfo>(pagedata);
    }

    //@RequestMapping(value="goods/{id}",method=RequestMethod.GET)
    @GetMapping("{id}/restflight")
    public JsonResult<Flightinfo> showGoodsById(@PathVariable String id){
        return new JsonResult<Flightinfo>(flightinfoMapper.selectOne(new Flightinfo().setPid(id)));
    }

    //@RequestMapping(value="goods",method=RequestMethod.POST)
    @PostMapping("restflight")
    public JsonResult addGoods(@RequestBody @Valid Flightinfo flightinfo, BindingResult result){
//		if(result.hasErrors()){
//			throw new SBMException(ErrorEnum.CHECK_FAIL,result.getFieldError().getDefaultMessage());
//		}
        flightinfoMapper.insertSelective(flightinfo);
        return new JsonResult("添加成功");


    }

    //@RequestMapping(value="goods",method=RequestMethod.PUT)
    @PutMapping("restflight")
    public JsonResult modifyGoods(@RequestBody @Valid Flightinfo flightinfo, BindingResult result){
//		if(result.hasErrors()){
//			throw new SBMException(ErrorEnum.CHECK_FAIL,result.getFieldError().getDefaultMessage());
//		}
        int row=flightinfoMapper.updateByPrimaryKey(flightinfo);
        System.out.println(row);
        return new JsonResult("修改成功");
    }

    //@RequestMapping(value="goods/{id}",method=RequestMethod.DELETE)
    @DeleteMapping("restflight")
    public JsonResult deleteGoods(@RequestBody List<Flightinfo> flightinfos){

        for(Flightinfo fi:flightinfos){
            flightinfoMapper.delete(fi);
        }
        return new JsonResult("删除成功");
    }
}
